//
//  FourthViewController3.m
//  UIhomework_14
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "FourthViewController3.h"
#import "RegisterModel.h"
#import "DataBaseHandle.h"

@interface FourthViewController3 ()<UITextFieldDelegate>
@property (nonatomic, retain) UITextField *tf;
@property (nonatomic, retain) UITextField *tf2;
@property (nonatomic, retain) UITextField *tf3;
@property (nonatomic, retain) UITextField *tf4;
@property (nonatomic, retain) UITextField *tf5;

@end

@implementation FourthViewController3

-(void)dealloc{
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"注册";
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 35, 100, 20)];
    label.text = @"用户名:";
    [self.view addSubview:label];
    [label release];
    
    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(150, 35, 200, 20)];
    _tf.layer.borderWidth = 1;
    _tf.layer.cornerRadius = 5;
    _tf.placeholder = @"请输入用户名";
    _tf.delegate = self;
    [self.view addSubview:_tf];
    [_tf release];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 85, 100, 20)];
    label2.text = @"密码:";
    [self.view addSubview:label2];
    [label2 release];
    
    self.tf2 = [[UITextField alloc] initWithFrame:CGRectMake(150, 85, 200, 20)];
    _tf2.layer.borderWidth = 1;
    _tf2.layer.cornerRadius = 5;
    _tf2.placeholder = @"请输入密码";
    _tf2.secureTextEntry = YES;
    _tf2.delegate = self;
    [self.view addSubview:_tf2];
    [_tf2 release];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(30, 135, 100, 20)];
    label3.text = @"确认密码:";
    [self.view addSubview:label3];
    [label3 release];
    
    self.tf3 = [[UITextField alloc] initWithFrame:CGRectMake(150, 135, 200, 20)];
    _tf3.layer.borderWidth = 1;
    _tf3.layer.cornerRadius = 5;
    _tf3.placeholder = @"再次输入密码";
    _tf3.secureTextEntry = YES;
    _tf3.delegate = self;
    [self.view addSubview:_tf3];
    [_tf3 release];
    
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(30, 185, 100, 20)];
    label4.text = @"邮箱:";
    [self.view addSubview:label4];
    [label4 release];
    
    self.tf4 = [[UITextField alloc] initWithFrame:CGRectMake(150, 185, 200, 20)];
    _tf4.layer.borderWidth = 1;
    _tf4.layer.cornerRadius = 5;
    _tf4.placeholder = @"请输入邮箱";
    _tf4.secureTextEntry = YES;
    _tf4.delegate = self;
    [self.view addSubview:_tf4];
    [_tf4 release];
    
    UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(30, 235, 100, 20)];
    label5.text = @"联系方式";
    [self.view addSubview:label5];
    [label5 release];
    
    self.tf5 = [[UITextField alloc] initWithFrame:CGRectMake(150, 235, 200, 20)];
    _tf5.layer.borderWidth = 1;
    _tf5.layer.cornerRadius = 5;
    _tf5.placeholder = @"请输入联系方式";
    _tf5.secureTextEntry = YES;
    _tf5.delegate = self;
    [self.view addSubview:_tf5];
    [_tf5 release];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)itemAction:(UIBarButtonItem *)item{
    RegisterModel *model = [RegisterModel registerModel];
    model.username = _tf.text;
    model.password = _tf2.text;
    model.againPassword = _tf3.text;
    model.mailbox = _tf4.text;
    model.phone = _tf5.text;
    [[DataBaseHandle shareDataBaseHandle] insertRegister:model];
    
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
